/*
 * To change this template, choose Tools | Templates
 * and opFen the template in the editor.
 */

package ru.staf.NetworkReportGenerator.V2.Panasonic;

import ru.staf.NetworkReportGenerator.V2.LeafDevice;
import ru.staf.NetworkReportGenerator.V2.HtmlUtil;
import ru.staf.NetworkReportGenerator.V2.ConfigParser;

import java.net.URL;
import java.util.Map;
import java.util.HashMap;

import java.io.IOException;


/**
 *
 * @author aezhov
 */
public class Camera extends LeafDevice {
    String model = null;
    String hostname = null;
    String noticeString = "КАМЕРА ";
    Map<String, String> variables = new HashMap<String, String>();
    
    public Camera(String html, String ip, String mac) {
        super(mac, ip);
        ipAddr = ip;
        variables.put("iinterval", null);
        variables.put("ijresol", null);
        variables.put("sjquality", null);
        variables.put("impeg", null);
        variables.put("impeg2", null);
        variables.put("imresol", null);
        variables.put("imresol2", null);
        variables.put("ibandwidth", null);
        variables.put("ibandwidth2", null);
        variables.put("smfr", null);
        variables.put("smfr2", null);
        variables.put("smquality", null);
        variables.put("smquality2", null);
        variables.put("sjresol", null);

        // Detect model
        int i1 = html.indexOf("networkcamera");
        int i2 = html.indexOf("<title>");
        model = html.substring(i2 + 7, i1).toUpperCase();
        System.out.println("Camera.<init>: " + ip + " Model: " + model);
        setHostname();
        String setImgHtml = null;
        for (Integer num : ConfigParser.logins.keySet()) {
            ConfigParser.LoginEntry le = ConfigParser.logins.get(num);
            try {
                setImgHtml= HtmlUtil.readURL("http://" + ip +
                    "/admin/setup_image.html", le.login, le.pass);
                getVariables(setImgHtml);
                break;
            } catch (IOException exc) {
                
            }
        }
        createNoticeSting();
    }

    public String getName() { return hostname; }

    public String getModel() { return model; }

    public String getVendor() { return "Panasonic"; }

    public String getSpecificNotice() { return noticeString; }
    
    // Private interface
    void setHostname() {
        String url = "http://" + ipAddr + "/live/titlebar_hide.html";
        String html = null;

        for (Integer num : ConfigParser.logins.keySet()) {
            ConfigParser.LoginEntry le = ConfigParser.logins.get(num);
            try {
                html = HtmlUtil.readURL(url, le.login, le.pass);
                break;
            } catch (IOException exc) {
                
            }
        }
        if (html != null) {
            String marker = "capi_changespecialsign(\"";
            int i1 = html.indexOf(marker);
            int i2 = html.indexOf("\")", i1 + marker.length());
            hostname = html.substring(i1 + marker.length(), i2);
            System.out.println("HOSTNAME: " + hostname);
        }
    }

    void getVariables(String htmlString) {
        int i1 = 0;

        while (true) {
            i1 = htmlString.indexOf("var", i1);
            if (i1 != -1) {
                i1 += 3;
                int i2 = htmlString.indexOf(";", i1);
                if (i2 < 0)
                    break;

                String expr = htmlString.substring(i1, i2);
                String[] tokens = expr.split("=");
                if (tokens.length < 2) {
                    i1 = i2 + 1;
                    continue;
                }

                String varname = tokens[0];
                String varval = tokens[1];
                if (variables.containsKey(varname)) 
                    variables.put(varname, varval);

                i1 = i2 + 1;
            } else
                break;
        }
    }

    void createNoticeSting() {
        System.out.println(variables);
        String interval = variables.get("iinterval");
        String resolutionX = variables.get("ijresol");
        if (resolutionX == null) {
            resolutionX = variables.get("sjresol");
            if (resolutionX.equals("\"max\""))
                resolutionX = "1280";
        }

        resolutionX = resolutionX.replaceAll("\"", "");
        int resolutionY = (int)(new Integer(resolutionX).intValue() * 0.75);
        String quality = variables.get("sjquality");

        noticeString += "MJPEG: " + interval + " кадр/с, " +
                resolutionX + "x" + resolutionY + ", качество " + quality;

        String mpeg41on = variables.get("impeg");
        String mpeg42on = variables.get("impeg2");

        if (mpeg41on != null) {
            if (mpeg41on.equals("0")) {
                noticeString += " MPEG4: выкл";
            }
        }

        if (mpeg42on != null) {
            if (mpeg42on.equals("0")) {
                noticeString += " MPEG4(2): выкл";
            }
        }
    }
}
